﻿using System.Collections.ObjectModel;
using FluentValidation;
using FluentValidation.Results;
using WarehouseManagementSystem.DAL;
using WarehouseManagementSystem.Entities;
using WarehouseManagementSystem.Extensions;
using WarehouseManagementSystem.Services.IServices;
using Wpf.Ui;

namespace WarehouseManagementSystem.ViewModels.Pages;

public partial class CargoViewModel(ISqliteService sqliteService, 
    INavigationService navigationService, 
    CargoRepository cargoRepository, 
    IValidator<Cargo> validator) : ObservableObject
{
    private bool _isInitialized;

    [ObservableProperty]
    public Cargo _cargo = new();

    [ObservableProperty]
    private ObservableCollection<Cargo> _cargoCollection = new();

    [ObservableProperty]
    private bool _isFlyoutOpen = false;

    [ObservableProperty]
    private string _warningMessage;


    [RelayCommand]
    private async Task LoadAsync()
    {
        if (!_isInitialized)
            CargoCollection.AddRange(await cargoRepository.GetAll());

        _isInitialized = true;
    }

    [RelayCommand]
    private async Task SaveAsync()
    {
        if (Cargo is null)
        {
            await ClearAsync();
        }

        //CreateTableResult createTableResult = await sqliteService.Connection.CreateTableAsync<Cargo>();

        //Cargo cargo = new Cargo {Id = 3, InsertDate = DateTime.Now, Name = "塑料", Tag = "材料"};

        ValidationResult validationResult = await validator.ValidateAsync(Cargo);
        
        if (!validationResult.IsValid && Cargo.Id == 0 && !string.IsNullOrWhiteSpace(Cargo.Name))
        {
            Cargo.Id = (await cargoRepository.GetAll()).Last().Id + 1;
            Cargo.InsertDate = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");

            await cargoRepository.Insert(Cargo);
            CargoCollection.Clear();
            CargoCollection.AddRange(await cargoRepository.GetAll());

            await ClearAsync();
        }
        else if (!validationResult.IsValid && Cargo.Id == 0 && string.IsNullOrWhiteSpace(Cargo.Name))
        {
            IsFlyoutOpen = true;

            WarningMessage = "物资名称不能为空！！";
        }
        else
        {
            IsFlyoutOpen = true;

            WarningMessage = "请先清空后再添加！";
        }
        
    }

    [RelayCommand]
    private async Task DeleteAsync(Cargo cargo)
    {
        if (cargo is not null && cargo.Id != 0)
        {
            await cargoRepository.Delete(cargo);

            var uiMessageBox = new Wpf.Ui.Controls.MessageBox
            {
                Title = "Success",
                Content = "删除成功~",
            };

            await uiMessageBox.ShowDialogAsync();

            await ClearAsync();
            CargoCollection.Clear();
            CargoCollection.AddRange(await cargoRepository.GetAll());
        }
        else
        {
            var uiMessageBox = new Wpf.Ui.Controls.MessageBox
            {
                Title = "Warning",
                Content = "请选住该行后再点击操作",
            };
            await uiMessageBox.ShowDialogAsync();
        }

    }

    [RelayCommand]
    private async Task UpdateAsync()
    {
        if (Cargo.Id != 0)
        {
            await cargoRepository.Update(Cargo);

            var uiMessageBox = new Wpf.Ui.Controls.MessageBox
            {
                Title = "Success",
                Content = "修改成功~",
            };

            await uiMessageBox.ShowDialogAsync();

            await ClearAsync();
            CargoCollection.Clear();
            CargoCollection.AddRange(await cargoRepository.GetAll());
        }
    }

    [RelayCommand]
    private async Task ClearAsync()
    {
        Cargo = new Cargo();

        IsFlyoutOpen = false;
    }

    [RelayCommand]
    private async Task QueryAsync(string cargoName)
    {
        if (string.IsNullOrWhiteSpace(cargoName))
        {
            CargoCollection.Clear();
            CargoCollection.AddRange(await cargoRepository.GetAll());

            _isInitialized = true;
        }
        else
        {
            CargoCollection.Clear();
            CargoCollection.AddRange(await cargoRepository.FindAll(cargoName));
        }
    }
}